<?php
/**
 * @Name: DelayedToken.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Http\Logic\Rbac\UserTokenLogic;

class DelayedToken
{

    public function handle(Request $request, Closure $next)
    {
        $response      = $next($request);
        // 文件流跳过 setContent 执行
//        $skipWhiteList = ['sample-stock.export'];
//        $currentRouter = implode('.', _controller_action());
//        !in_array($currentRouter, $skipWhiteList) &&

        if ($request->delayed && $request->userId) {
            $content          = json_decode($response->getContent(), true);
            $content['token'] = UserTokenLogic::instance()->createToken($request->userId);
            $response->setContent(json_encode($content));
        }
        return $response;
    }

}
